﻿@using viBook.Common
@using viBook.Models
@model IEnumerable<Book>

@{
    var totalCommentPage = (int)Math.Ceiling((double)Model.Count() / Constant.MaximunItemsToPaging);
    totalCommentPage = totalCommentPage == 0 ? 1 : totalCommentPage;
    bool isShowNextButton = totalCommentPage > 1;
    var listMostPopularTags = ViewBag.ListMostPopularTags as IEnumerable<ContentTag>;
    var listMostPopularBook = ViewBag.ListMostPopularBook as IEnumerable<Book>;
    var listMostPopularUser = ViewBag.ListMostPopularUser as IEnumerable<User>;
}

<script type="text/javascript">

var listBook = @CustomHelpers.BooksToJavaScriptConverter(@Model.Take(Constant.MaximunItemsToPaging).ToList()) ,
        listTags = @CustomHelpers.ContentTagsToJavaScriptConverter(listMostPopularTags.ToList()) ,
     listshortBook = @CustomHelpers.BooksToJavaScriptConverter(listMostPopularBook.ToList()) ,
     listUsers = @CustomHelpers.UserToJavaScriptConverter(listMostPopularUser.ToList()) ,
    viewmodel = {
        totalBookPage: ko.observable( @totalCommentPage ),
                  listTags: ko.observableArray(listTags),
         listShortBook: ko.observableArray(listshortBook),
         listUsers: ko.observableArray(listUsers),
        isShowPrevBtn: ko.observable(false),
        isShowNextBtn: ko.observable( @isShowNextButton.ToString().ToLower() ),
        numberOfComments: ko.observable( @Model.Count() ),
        bookPageNo : ko.observable(1),
        listBook: ko.observableArray(listBook),
        fetchNextCommentPage: function() {
            $.ajax({
                    url: '@Url.Action("FetchNextCommentPage", "Books")',
                    type: 'POST',
                    data: ko.toJSON({  pageNumber: viewmodel.bookPageNo() + 1 }),
                    contentType: 'APPLICATION/JSON',
                    success: function(result) {
                        if (result.isSuccess) {
                            var listBooks = [];
                            $(result.books).each(function(index, item) {
                                listBooks[index] = new Book(item.id, item.title, item.coverImage, item.hasChapter, item.numberOfPages, item.numberOfChapters, item.numberOfLikes, item.numberOfViews, item.numberOfComments,item.uploader,item.uploadedBy,item.uploadedDateString) ;
                            });
                            viewmodel.listBook(listBooks);
                            viewmodel.commentPageNo(viewmodel.bookPageNo() + 1);
                            viewmodel.isShowPrevBtn(viewmodel.bookPageNo() > 1);
                            viewmodel.isShowNextBtn(viewmodel.bookPageNo() < viewmodel.totalBookPage());
                        } else {
                            alert(result.message);
                        }
                    }
                });
        },
        fetchPreviousCommentPage: function() {
            $.ajax({
                    url: '@Url.Action("FetchNextCommentPage", "Books")',
                    type: 'POST',
                    data: ko.toJSON({ pageNumber: viewmodel.bookPageNo() - 1 }),
                    contentType: 'APPLICATION/JSON',
                    success: function(result) {
                        if (result.isSuccess) {
                            var listBooks = [];
                            $(result.books).each(function(index, item) {
                                listBooks[index] = new Book(item.id, item.title, item.coverImage, item.hasChapter, item.numberOfPages, item.numberOfChapters, item.numberOfLikes, item.numberOfViews, item.numberOfComments,item.uploader,item.uploadedBy,item.uploadedDateString) ;
                            });
                            viewmodel.listBook(listBooks);
                            viewmodel.commentPageNo(viewmodel.bookPageNo() - 1);
                            viewmodel.isShowPrevBtn(viewmodel.bookPageNo() > 1);
                            viewmodel.isShowNextBtn(viewmodel.bookPageNo() < viewmodel.totalBookPage());
                        } else {
                            alert(result.message);
                        }
                    }
                });
        }
    };
    ko.applyBindings(viewmodel);
</script>


